package com.liu.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.liu.sunnyday.constant.MessageConstant;
import com.liu.sunnyday.entity.PageResult;
import com.liu.sunnyday.entity.QueryPageBean;
import com.liu.sunnyday.entity.Result;
import com.liu.sunnyday.pojo.CheckGroup;
import com.liu.sunnyday.pojo.CheckItem;
import com.liu.sunnyday.service.CheckItemService;
import com.liu.sunnyday.service.CheckgroupService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * chance！Time
 * ：[体检检查组]
 *
 * @author liuqian
 * @date 2022-06-08 12:17
 * *
 */
@RestController
@RequestMapping("/checkgroup")
public class CheckgroupController {
    @Reference
    private CheckgroupService checkgroupService;


//    /*查询所有*/
//    @GetMapping("/findAll")
//    public Result findAll() {
//        List<CheckItem> list = checkItemService.findAll();
//        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
//    }


    /*1-分页查询所有*/
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        //调用服务分页查询
        PageInfo<CheckGroup> pageInfo = checkgroupService.findPage(queryPageBean);
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, pageResult);
    }


    /**
    * 2-新增检查组
     * 参数为新增表单中的检查组数据，和复选框中的检查项id
     */
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        checkgroupService.add(checkGroup, checkitemIds);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }


    //3-编辑检查组---查询检查组数据回显
    @GetMapping("/findById")
    public Result findById(Integer id) {
        CheckGroup checkGroup = checkgroupService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
    }
    //3-编辑检查组---查询检查项ids回显复选框
    @GetMapping("/findByitemid")
    public Result findByitemid(Integer id) {
        List<Integer> listCheckId = checkgroupService.findByCheckID(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, listCheckId);
    }
    //4-编辑检查组--确定提交表单
    @PostMapping("/update")
    public Result update(@RequestBody CheckGroup CheckGroup, Integer[] checkitemIds) {
        checkgroupService.update(CheckGroup, checkitemIds);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    //5-删除检查组
    @PostMapping("/deleteById")
    public Result deleteById(Integer id) {
        //System.out.println(id);
        checkgroupService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

}
